package concurrent.c_001_Synchronized;

import util.ThreadUtil;

/**
 * 同步和非同步方法是否可以同时调用？
 */
public class T06_SyncOrNoSync {
    public static void main(String[] args) {
        T06_SyncOrNoSync t = new T06_SyncOrNoSync();

        // new Thread(()->t.m1(), "t1").start();
        // new Thread(()->t.m2(), "t2").start();

        new Thread(t::m1, "t1").start();
        new Thread(t::m2, "t2").start();

        // 1.8之前的写法
        // new Thread(new Runnable() {
        //     @Override
        //     public void run() {
        //         t.m1();
        //     }
        // });
    }

    public synchronized void m1() {
        System.out.println(Thread.currentThread().getName() + " m1 start...");
        ThreadUtil.sleepMillis(1000);
        System.out.println(Thread.currentThread().getName() + " m1 end");
    }

    public void m2() {
        ThreadUtil.sleepMillis(500);
        System.out.println(Thread.currentThread().getName() + " m2 ");
    }
}